package ie.lawlor.amvc.event;

import ie.lawlor.amvc.IController;

/**
 * @author blawlor
 *
 * Represents the events passed between hMVC triads.
 */
public class Event {
	// TODO Store the name and namespace as an EventName (can reuse toString etc this way)
	private String name;
	private String nameSpace = EventName.NONAMESPACE;
	private Object source;
	private IController sourceController;
	private Object payload;
	

    /**
     * @param name
     * @param source
     * @param payload
     */
    public Event(String name, Object source, Object payload) {
        this.name = name;
        this.source = source;
        this.payload = payload;
    }  
    
    /**
     * @param name
     * @param source
     */
    public Event(String name, Object source) {
        this.name = name;
        this.source = source;
    }
    
	public Event(EventName eventName, Object source){
		this.name = eventName.name;
		this.nameSpace = eventName.nameSpace;
		this.source = source;
	}
	
	public Event(EventName eventName, Object source, Object payload){
		this.name = eventName.name;
		this.payload = payload;
		this.nameSpace = eventName.nameSpace;
		this.source = source;
	}
	
    public Event(Event e){
    	this.name = e.name;
    	this.nameSpace = e.nameSpace;
    	this.source = e.source;
    	this.sourceController = e.sourceController;
    	this.payload = e.payload;
    }
    
    public void setSourceController(IController sourceController){
    	this.sourceController = sourceController;
    }
    
    public IController getSourceController(){
    	return sourceController;
    }
    
    
    /**
     * @return Namespace of event
     */
    public String getNameSpace() {
        return nameSpace;
    }

    /**
     * @param nameSpace Name space of event
     */
    public void setNameSpace(String nameSpace) {
        this.nameSpace = nameSpace;
    }

    /**
     * @return
     */
    public String getName() {
        return name;
    }
    
	public EventName getEventName(){
		return new EventName(nameSpace, name);
	}

	public void setEventName(EventName eventName){
		this.name = eventName.name;
		this.nameSpace = eventName.nameSpace;
	}

    /**
     * @return
     */
    public Object getPayload() {
        return payload;
    }
    
    public void setPayload(Object payload){
      this.payload = payload;
    }

    /**
     * @return
     */
    public Object getSource() {
        return source;
    }

	public String toString(){
		return nameSpace + "::" + name;
	}
	
	public boolean equals(Object o){
		if (o instanceof Event){
			Event e = (Event)o;
			return e.name.equalsIgnoreCase(name);
		} else {
			return false;
		}
	}
	
	public int hashCode(){
		return name.hashCode();
	}
}
